<template>
	<div class="layout-padding">
		<div class="layout-padding-auto layout-padding-view">
			<el-row shadow="hover" v-show="showSearch" class="ml10">
				<el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="getDataList">
					<el-form-item label="状态" prop="status">
						<el-select v-model="state.queryForm.status" placeholder="请选择状态">
							<el-option :key="1" label="进行中" :value="1" />
							<el-option :key="2" label="已结束" :value="2" />
						</el-select>
					</el-form-item>
					<el-form-item label="发起时间" prop="taskTime">
						<el-date-picker
							type="datetimerange"
							value-format="YYYY-MM-DD HH:mm:ss"
							v-model="state.queryForm.taskTime"
							is-range
							range-separator="To"
						/>
					</el-form-item>
					<el-form-item>
						<el-button icon="search" type="primary" @click="getDataList">
							{{ $t('common.queryBtn') }}
						</el-button>
						<el-button icon="Refresh" @click="resetQuery">{{ $t('common.resetBtn') }}</el-button>
					</el-form-item>
				</el-form>
			</el-row>

			<el-row>
				<div class="mb8" style="width: 100%">
					<right-toolbar
						v-model:showSearch="showSearch"
						class="ml10"
						style="float: right; margin-right: 20px"
						@queryTable="getDataList"
					></right-toolbar>
				</div>
			</el-row>

			<el-table
				ref="dataTableRef"
				v-loading="loading"
				:data="state.dataList"
				highlight-current-row
				border
				:cell-style="tableStyle.cellStyle"
				:header-cell-style="tableStyle.headerCellStyle"
			>
				<el-table-column label="分组" prop="groupName" width="100" />
				<el-table-column label="流程" prop="name" width="150" />
				<el-table-column label="发起时间" prop="createTime" width="200" />
				<el-table-column label="结束时间" prop="endTime" width="200" />
				<el-table-column label="状态" prop="taskCreateTime" width="200">
					<template #default="scope">
						<el-tag v-if="scope.row.status == 1">进行中</el-tag>
						<el-tag v-else-if="scope.row?.finishReason == '9'" type="danger">终止</el-tag>
						<el-tag v-else-if="scope.row?.finishReason == '1'" type="success">通过</el-tag>
						<el-tag v-else-if="scope.row?.finishReason == '0'" type="danger">拒绝</el-tag>
						<el-tag v-else>已结束</el-tag>
					</template>
				</el-table-column>

				<el-table-column fixed="right" label="操作">
					<template #default="scope">
						<el-button type="primary" size="small" link icon="View" @click="deal(scope.row)"> 查看 </el-button>
						<el-button :disabled="scope.row.status != 1" type="primary" size="small" link icon="VideoPause" @click="stop(scope.row)">
							终止流程
						</el-button>
					</template>
				</el-table-column>
			</el-table>

			<pagination @current-change="currentChangeHandle" @size-change="sizeChangeHandle" v-bind="state.pagination"></pagination>
			<!--			右侧抽屉-->
			<el-drawer v-model="rightDrawerVisible" v-if="rightDrawerVisible" direction="rtl" size="50%" destroy-on-close>
				<template #header>
					<h3>{{ currentData?.name }}</h3>
				</template>
				<template #default>
					<el-row>
						<el-col :span="16">
							<el-form label-position="top">
								<!-- 情况一：动态表单 -->
								<div v-if="!dynamicFormComponent">
									<FormCreate :rule="rule" v-model="formData" v-model:api="fApi" />
								</div>
								<!-- 情况二：自定义业务表单 -->
								<div v-else>
									<component 
										:is="dynamicFormComponent.component" 
										v-bind="dynamicFormComponent.props"
									/>
								</div>
							</el-form>
						</el-col>
						<el-col :span="8">
							<flow-node-format
								:disableSelect="true"
								:processInstanceId="currentData.processInstanceId"
								:flow-id="currentData.flowId"
								ref="flowNodeFormatRef"
							/>
						</el-col>
					</el-row>
				</template>
			</el-drawer>
		</div>
	</div>
</template>
<script setup lang="ts">
import FlowNodeFormat from '/@/views/flow/form/tools/FlowNodeFormatData.vue';
import { queryMineStarted, stopProcessInstance } from '/@/api/flow/task';
import { detail } from '/@/api/flow/processInstance';
import { BasicTableProps, useTable } from '/@/hooks/table';

import FcDesigner from 'form-create-designer';
import { processFormItemsWithPerms } from '/@/views/flow/workflow/utils/formPermissions';
import FormCreate from '/@/views/flow/workflow/components/FormCreate.vue';
import { useMessage } from '/@/hooks/message';
import { createDynamicFormComponent, type DynamicFormComponent } from '/@/views/flow/workflow/utils/dynamicComponent';

const rule = ref([]);
const fApi = ref();
const formData = ref({});
const dynamicFormComponent = shallowRef<DynamicFormComponent | null>(null); // 动态表单组件

const rightDrawerVisible = ref(false);

const loading = ref(false);
const showSearch = ref(true);
const queryRef = ref();
const state: BasicTableProps = reactive<BasicTableProps>({
	pageList: queryMineStarted,
	queryForm: {
		taskTime: undefined,
		status: 1,
	},
});

const { tableStyle, getDataList, currentChangeHandle, sortChangeHandle, sizeChangeHandle } = useTable(state);
function stop(row) {
	stopProcessInstance({
		processInstanceId: row.processInstanceId,
	}).then((res) => {
		useMessage().success('流程终止成功');
		getDataList();
	});
}

const currentData = ref();
/**
 * 点击开始处理
 * @param row
 */
const deal = (row) => {
	currentData.value = row;
	detail({
		processInstanceId: row.processInstanceId,
	}).then((res) => {
		const { formItems, formPerms, formData: responseFormData, formConfig,processInstanceId } = res.data;

		// 重置动态表单组件
		dynamicFormComponent.value = null;

		// 检查是否有formConfig，判断表单类型
		if (formConfig) {
			const dynamicComponent = createDynamicFormComponent(formConfig, processInstanceId, true);
			if (dynamicComponent) {
				// 使用自定义业务表单
				dynamicFormComponent.value = dynamicComponent;
			} else {
				// 使用动态表单
				setupDynamicForm(formItems, formPerms, responseFormData);
			}
		} else {
			// 兼容旧版本，使用动态表单
			setupDynamicForm(formItems, formPerms, responseFormData);
		}

		rightDrawerVisible.value = true;
	});
};

// 设置动态表单的辅助函数
const setupDynamicForm = (formItems: any, formPerms: any, responseFormData: any) => {
	// 解析表单项并处理权限
	const parsedFormItems = FcDesigner.formCreate.parseJson(formItems);
	const itemsWithPerms = processFormItemsWithPerms(parsedFormItems, formPerms);

	rule.value = itemsWithPerms;
	formData.value = FcDesigner.formCreate.parseJson(responseFormData);
	currentOpenFlowForm.value = formItems;
};
const currentOpenFlowForm = ref();

// 清空搜索条件
const resetQuery = () => {
	queryRef.value.resetFields();
	getDataList();
};

onMounted(() => {
	getDataList();
});
</script>
